<template>
    
    <!-- 组件的模板 -->
    <div class="page signin-manage">
        <!-- <div class="weui-cells__title">上报设置</div> -->
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell weui-cell_switch">
                <div class="weui-cell__bd">位置上报</div>
                <div class="weui-cell__ft">
                    <input class="weui-switch" type="checkbox" v-model="sign_switch" :true-value="1" :false-value="0">
                </div>
            </div>
            <template class="weui-cells" v-if="sign_switch == 1">
                <a class="weui-cell weui-cell_access weui-cell_setupsign" href="javascript:;" @click="setupRequire">
                    <div class="weui-cell__bd">
                        <p>填写项设置</p>
                    </div>
                    <div class="weui-cell__ft">{{require_word}}</div>
                </a>
                <a class="weui-cell weui-cell_access" href="javascript:;" @click="gotoRemind">
                    <div class="weui-cell__bd rgb_51">
                        <p>提醒设置</p>
                    </div>
                    <div class="weui-cell__ft">{{remind_info}}
                    </div>
                </a>
                <!-- <div class="weui-cell weui-cells_checkbox">
                    <div class="weui-cell__hd">填写项设置</div>
                    <label class="weui-cell weui-check__label" for="s11">
                        <div class="weui-cell__hd">
                            <input type="checkbox" class="weui-check" name="checkbox1" id="s11" v-model="photo">
                            <i class="weui-icon-checked"></i>
                        </div>
                        <div class="weui-cell__bd">
                            <p>拍照</p>
                        </div>
                    </label>
                    <label class="weui-cell weui-check__label" for="s12">
                        <div class="weui-cell__hd">
                            <input type="checkbox" class="weui-check" name="checkbox1" id="s12" v-model="remark">
                            <i class="weui-icon-checked"></i>
                        </div>
                        <div class="weui-cell__bd">
                            <p>备注</p>
                        </div>
                    </label>
                </div> -->
                <!-- <div class="weui-cell weui-cell_switch">
                    <div class="weui-cell__bd">允许地点微调</div>
                    <div class="weui-cell__ft">
                        <input class="weui-switch" type="checkbox" v-model="position_tune">
                    </div>
                </div> -->
                <!-- <a class="weui-cell weui-cell_access" href="javascript:;" @click="changeTuneDistance">
                    <div class="weui-cell__bd">
                        <p>地点微调</p>
                    </div>
                    <div class="weui-cell__ft">{{tune_distance == 0 ? '关闭' : tune_distance + '米'}}</div>
                </a>
                <div class="weui-cell weui-cell_switch">
                    <div class="weui-cell__bd">允许使用相册图片</div>
                    <div class="weui-cell__ft">
                        <input class="weui-switch" type="checkbox" v-model="allow_album" :true-value="1" :false-value="0">
                    </div>
                </div> -->
            </template>
        </div>
        <!-- <div class="weui-cells__signtip">员工主动上报位置，可通过工作助手APP、微信上报</div> -->
        <!-- 轨迹监控 -->
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell weui-cell_switch">
                <div class="weui-cell__bd">轨迹监控</div>
                <div class="weui-cell__ft">
                    <input class="weui-switch" type="checkbox" v-model="trace_switch" :true-value="1" :false-value="0">
                </div>
            </div>
        </div>
        <div class="weui-cells__signtip">员工使用APP时，根据考勤时间自动记录工作轨迹，若无考勤规默认记录6:00 ~ 18:00的轨迹</div>
        <!-- <div class="weui-cells__title">现场点到</div>
        <div class="weui-cells">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="gotoLocaleSignin">
                <div class="weui-cell__bd">
                    <p>开始现场点到</p>
                </div>
                <div class="weui-cell__ft">
                </div>
            </a>            
        </div> -->
    </div>
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import widget from 'gapi/widget'
    import clientUi from 'gapi/clientUi'
    import * as util from 'js/util.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                user_info_sign: {
                    team_id: 0,
                    project_id: 0,
                    user_id: 0,
                    remind_type: 0, //1-特定日期 2-固定时间段
                    remind_date: [],
                    remind_time: [],
                    type: 2,//edit 2 , create 1
                    lastPageType: 2,//上一页来源：1-表单 2-签到
                },
                sign_switch: false,
                require_field_formid: 0,
                setting_data: {
                    require_field: [
                        {
                            "id": '',
                            "type": "Location",
                            "name": "位置",
                            "info": {
                                "title": "位置",
                                "value": {
                                    "lat": "",
                                    "lng": "",
                                    "addr": ""
                                },
                                "tune_distance":500,
                                "tip": "",
                                "required": 1
                            },
                            order: 1
                        }
                    ]
                },
                remind_info: [],
                trace_switch: false,//轨迹监控
            }
        },
        computed: {
            // allow_album: function(){//是否允许选择相册图片 0：不允许 1：允许，不传或空表示不允许
            //     return this.allow_album1 == 1 ? 1 : 0
            // },
            // position_tune: function(){
            //     return this.position_tune1 == 1 ? 1 : 0
            // },
            // require_field: function(){
            //     let tempObj = {}
            //     tempObj.photo = this.photo ? 1 : 0
            //     tempObj.remark = this.remark ? 1 : 0
            //     return tempObj
            // },
            require_word() {
                let str = ''
                this.setting_data.require_field.forEach((obj) => {
                    str += `、${obj.info.title}`
                })
                return str.substr(1)
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                //获取用户信息
                cache.getCache('user_info_sign', (data) => {
                    this.user_info_sign.team_id = data.team_id
                    this.user_info_sign.project_id = data.project_id
                    this.user_info_sign.user_id = data.user_id

                    // return
                    //获取项目签到配置
                    commonPage.loadPageStatus('loading')
                    util.ajax({
                        url: '/sign/project/setting',
                        type:'GET',
                        data: {
                            team_id: this.user_info_sign.team_id,
                            project_id: this.user_info_sign.project_id
                        },
                        success: (res) => {
                            // alert(JSON.stringify(res))
                            console.log("获取项目签到配置"+JSON.stringify(res))
                            commonPage.loadPageStatus('loadSuccess')
                            if(res&&res.errno==0){
                                // this.allow_album = res.data.setting_data.allow_album
                                this.sign_switch = res.data.setting_data.switch
                                this.trace_switch = res.data.setting_data.trace_switch

                                // this.position_tune = res.data.setting_data.position_tune
                                // this.tune_distance = !res.data.setting_data.tune_distance? 500 : Number(res.data.setting_data.tune_distance)
                                // this.photo = res.data.setting_data.require_field.photo == 1 ? true : false
                                // this.remark = res.data.setting_data.require_field.remark == 1 ? true : false
                                if(res.data.setting_data.require_field_formid&&res.data.setting_data.require_field_formid != 0){
                                    this.require_field_formid = res.data.setting_data.require_field_formid
                                    this.setting_data.require_field = res.data.setting_data.require_field
                                }
                                //获取提醒信息
                                this.user_info_sign.remind_time = res.data.setting_data.remind_time ? res.data.setting_data.remind_time : []
                                if(this.user_info_sign.remind_time.length == 0){
                                    this.remind_info = '不提醒'
                                }else if(this.user_info_sign.remind_time.length == 1){
                                    this.remind_info = this.user_info_sign.remind_time
                                }else if(this.user_info_sign.remind_time.length == 2){
                                    this.remind_info = this.user_info_sign.remind_time[0] + '、' + this.user_info_sign.remind_time[1] 
                                }else if(this.user_info_sign.remind_time.length > 2){
                                    this.remind_info = this.user_info_sign.remind_time[0] + '、' + this.user_info_sign.remind_time[1] + '...'
                                }
                                

                                this.doWatch()
                            }else if(res.errmsg){
                                 widget.toast(res.errmsg)
                            }else{
                                widget.toast('数据获取失败')
                            }
                        },
                        error: (xhr, error) => {
                            console.log(xhr)
                            console.log(error)
                            commonPage.loadPageStatus('loadSuccess')
                            widget.toast('服务器异常')
                        },
                        noNetwork: () => {
                            // 无网络的回调
                            commonPage.loadPageStatus('loadSuccess')
                            widget.toast('网络连接失败，请检查网络')
                        }
                    })
                })
            },
            submit(){
                let ajaxData = {
                        team_id: this.user_info_sign.team_id,
                        project_id: this.user_info_sign.project_id,
                        // allow_album: this.allow_album,
                        switch: this.sign_switch,
                        require_field: JSON.stringify(this.setting_data.require_field),
                        remind_time: this.user_info_sign.remind_time,
                        trace_switch: this.trace_switch
                    }
                console.log("位置上报设置提交数据"+JSON.stringify(ajaxData))
                // return
                //设置项目签到配置
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url: '/sign/project/deal',
                    type:'POST',
                    data: ajaxData,
                    success: (res) => {
                        // alert(JSON.stringify(res))
                        console.log(res)
                        commonPage.loadPageStatus('loadSuccess')
                        if(res&&res.errno==0){
                            if(this.sign_switch == 0){
                                //埋点
                                let statisticsLog_data = {
                                    event_id: '位置上报管理-关闭位置上报',
                                    // event_label: data.event_label,(预留，现在不用传此参数)
                                    event_param: {
                                        ca_from: 'closeposition',
                                        project_id: this.user_info_sign.project_id,
                                    }
                                }
                                // alert(JSON.stringify(statisticsLog_data))
                                KCApiApp.statisticsLog(statisticsLog_data)
                            }else if(this.sign_switch == 1){
                                //埋点
                                let statisticsLog_data = {
                                    event_id: '位置上报管理-开启位置上报',
                                    // event_label: data.event_label,(预留，现在不用传此参数)
                                    event_param: {
                                        ca_from: 'openpositon',
                                        project_id: this.user_info_sign.project_id,
                                    }
                                }
                                // alert(JSON.stringify(statisticsLog_data))
                                KCApiApp.statisticsLog(statisticsLog_data)
                            }
                            widget.toast('设置成功')
                        }else if(res.errmsg){
                             widget.toast(res.errmsg)
                        }else{
                            widget.toast('数据获取失败')
                        }
                    },
                    error: (xhr, error) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络,设置失败')
                    }
                })
            },
            changeTuneDistance(){
                let tune_distances = {
                    data1: ['关闭', 100, 200, 500, 1000],
                    // data2: []
                }
                let index = tune_distances.data1.indexOf(this.tune_distance == 0 ? '关闭' : this.tune_distance)
                let select = [index]
                clientUi.showPicker(tune_distances, select, "请选择微调范围", (data) => {
                    // let t = time.data1[data.data1] + ':' + time.data2[data.data2]
                    // alert(JSON.stringify(data))
                    if(data&&data.data1){
                        if(data.data1 == 0){
                            this.position_tune = 0
                            this.tune_distance = 0
                        }else{
                            this.position_tune = 1
                            this.tune_distance = tune_distances.data1[data.data1]
                        }
                    }
                    // alert(this.position_tune)
                    // alert(this.tune_distance)
                })
            },
            gotoLocaleSignin(){
                location.href = `dmsaas://SceneSign`
            },
            doWatch(){
                // this.$watch('allow_album', (newVal, oldVal) => {
                //     if(newVal != oldVal){
                //         this.submit()
                //     }
                // })
                this.$watch('sign_switch', (newVal, oldVal) => {
                    if(newVal != oldVal){
                        this.submit()
                    }
                })
                this.$watch('trace_switch', (newVal, oldVal) =>{
                    if(newVal != oldVal){
                        this.submit()
                    }
                })
                // this.$watch('require_field', (newVal, oldVal) => {
                //     if(newVal != oldVal){
                //         this.submit()
                //     }
                // })
                // this.$watch('position_tune', (newVal, oldVal) => {
                //     if(newVal != oldVal){
                //         // if(newVal == 1){
                //             if(this.tune_distance == 200){
                //                 this.submit()
                //             }else{
                //                 this.tune_distance = 200
                //             }
                //         // }else{
                //             // this.tune_distance = 200
                //         // }
                        
                //     }
                // })
                // this.$watch('tune_distance', (newVal, oldVal) => {
                //     if(newVal != oldVal){
                //         this.submit()
                //     }
                    
                // })
            },
            setupRequire(){
                //埋点
                let statisticsLog_data = {
                    event_id: '位置上报管理-填写项设置',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'setitem',
                        project_id: this.user_info_sign.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)

                cache.setCache('designInfo', this.setting_data.require_field, 10000)
                location.href = `dmsaas://SetupRequire`
            },
            gotoRemind (){
                //埋点
                let statisticsLog_data = {
                    event_id: '位置上报管理-提醒设置',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'setpush',
                        project_id: this.user_info_sign.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)
                let setupFormInfo = JSON.stringify(this.user_info_sign)
                let encodeStr = encodeURIComponent(`setupFormInfo=${setupFormInfo}`)
                location.href = `dmsaas://SetupFormRemind?urdData=${encodeStr}`


                // let url = `tid=${this.teamId}&pid=${this.projectId}&gid=${this.groupId}&uid=${this.userId}`
                // location.href="dmsaas://ProjectSearchMember?urdData="+encodeURIComponent(url)
            }
        },
        created(){

            this.init();

            commonPage.updateTitleBar({
                backAction: 'delselect',
                // leftDisplay: false,
                // leftText: '',
                leftAction: 'delselect',
                // rightDisplay: true,
                // rightText: '提交',
                // rightAction: 'doSubmit'
            })
            window.delselect = () => {
                cache.setMemoryCache('participantMember', [])
                commonPage.goBack(1)
            }
            window.loadData = (data) => {
                // console.log(data)
                if(data !== undefined){
                    if(data.key === "requireField"){
                        // alert(JSON.stringify(data.require_field))
                        this.setting_data.require_field = data.require_field
                        this.submit()
                    }else{
                        this.user_info_sign.remind_type = data.remind_type;
                        this.user_info_sign.remind_date = data.remind_date;
                        this.user_info_sign.remind_time = data.remind_time;
                        if(this.user_info_sign.remind_time.length == 0){
                            this.remind_info = '不提醒'
                        }else if(this.user_info_sign.remind_time.length == 1){
                            this.remind_info = this.user_info_sign.remind_time
                        }else if(this.user_info_sign.remind_time.length == 2){
                            this.remind_info = this.user_info_sign.remind_time[0] + '、' + this.user_info_sign.remind_time[1] 
                        }else if(this.user_info_sign.remind_time.length > 2){
                            this.remind_info = this.user_info_sign.remind_time[0] + '、' + this.user_info_sign.remind_time[1] + '...'
                        }
                        this.submit()
                    }
                    
                }
                
            }
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/attendance.css"></style>